package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.SmsDictionaryMapper;
import com.macro.mall.model.SmsDictionary;
import com.macro.mall.model.SmsDictionaryExample;
import com.macro.mall.service.SmsDictionaryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author 郎
 */
@Service
@AllArgsConstructor
public class SmsDictionaryServiceImpl implements SmsDictionaryService {
    private final SmsDictionaryMapper smsDictionaryMapper;

    /**
     * 查询所有专题
     */
    @Override
    public List<SmsDictionary> listAll() {
        SmsDictionaryExample example = new SmsDictionaryExample();
        example.createCriteria();
        return smsDictionaryMapper.selectByExample(example);
    }

    @Override
    public List<SmsDictionary> getListByType(String codeType) {
        SmsDictionaryExample example = new SmsDictionaryExample();
        example.createCriteria().andCodeEqualTo(codeType);
        List<SmsDictionary> dictionaryList = smsDictionaryMapper.selectByExample(example);
        if (!dictionaryList.isEmpty()) {
            SmsDictionary smsDictionary = dictionaryList.get(0);
            long pid = smsDictionary.getId();
            example.clear();
            example.createCriteria().andPidEqualTo(pid);
            example.setOrderByClause("sort asc");
            return smsDictionaryMapper.selectByExample(example);
        }
        return Collections.emptyList();
    }

    @Override
    public List<SmsDictionary> list(String code, String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        SmsDictionaryExample example = new SmsDictionaryExample();
        SmsDictionaryExample.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotBlank(code)) {
            criteria.andCodeLike("%" + code + "%");
        }
        if (StrUtil.isNotBlank(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andPidEqualTo(0L);
        example.setOrderByClause("last_modified_time desc");
        return smsDictionaryMapper.selectByExample(example);
    }

    @Override
    public List<SmsDictionary> listChildren(Long pid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        SmsDictionaryExample example = new SmsDictionaryExample();
        example.createCriteria().andPidEqualTo(pid);
        example.setOrderByClause("last_modified_time desc");
        return smsDictionaryMapper.selectByExample(example);
    }

    @Override
    public int delete(Long id) {
        return smsDictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteCurrentAndChildren(Long id) {
        SmsDictionaryExample example = new SmsDictionaryExample();
        example.createCriteria().andIdEqualTo(id);
        example.or().andPidEqualTo(id);
        return smsDictionaryMapper.deleteByExample(example);
    }

    @Override
    public int create(SmsDictionary smsDictionary) {
        return smsDictionaryMapper.insertSelective(smsDictionary);
    }

    @Override
    public int update(SmsDictionary smsDictionary) {
        return smsDictionaryMapper.updateByPrimaryKey(smsDictionary);
    }

    @Override
    public SmsDictionary detail(Long id) {
        return smsDictionaryMapper.selectByPrimaryKey(id);
    }
}
